package ui.videos 
{
	import com.asual.swfaddress.SWFAddress;
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IBitmapSprite;
	import com.greensock.TweenMax;
	import com.video.VideoPlayer;
	import com.video.VideoPlayerEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import uk.soulwire.utils.display.Alignment;
	import uk.soulwire.utils.display.DisplayUtils;
	import utils.ShapeUtil;
	import utils.StringUtils;
	
	/**
	 * ...
	 * @author usual
	 */
	public class SlideItem extends MovieClip 
	{
		private var _obj:*;
		private var _videoPlayer:VideoPlayer;
		private var _playHeadPercentage:Number;
		private var _playHeadTime:Number;
		private var _bi:BufferingIcon;
		public function SlideItem(obj:*) 
		{
			_obj = obj;
			addEventListener(Event.ADDED_TO_STAGE, build);
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		private function removedFromStage(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			trace("[SlideItem.removedFromStage] ");
		}
		
		public function destroy():void 
		{
			stage.removeEventListener(Event.RESIZE, _onResize);
			_obj.visible = false;
			if (_videoPlayer)
			{
				_videoPlayer.removeEventListener(VideoPlayerEvent.VIDEO_COMPLETE, _videoComplete);
				_videoPlayer.destroy();
			}
			if (this.parent != null) parent.removeChild(this);
		}
		
		public function initVideo():void 
		{
			var assetSrc:String = _obj.src;
			var lp:String = StringUtils.between(assetSrc, '/', '/jpeg')
			var lc:String = StringUtils.afterLast(lp, '/');
			var assetPathVideo:String = Gaia.api.getPage(Gaia.api.getCurrentBranch()).assetPath;
			var srcVideo:String;
			if (lc == "")
			{
				srcVideo = assetPathVideo +'/' + this.name + '.smil';
			}
			else
			{
				var bUrl:String =  SWFAddress.getBaseURL();
				if (bUrl == "http://localhost/webdoc") srcVideo = "php/smil.smil?l=" + lc;
				else srcVideo = "smil.smil?l=" + lc;
			}
			_videoPlayer.setSource(srcVideo);
		}
		
		public function pauseVideo():void 
		{
			//_videoPlayer.visible = false;
			_videoPlayer.pause();
		}
		
		public function unPauseVideo():void 
		{
			//_videoPlayer.visible = true;
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_READY));
			//TweenMax.to(_obj, 1, { alpha:0} );
			_videoPlayer.unPausePlayer();
		}
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);		
			this.cacheAsBitmap = true;
			
			IBitmapSprite(_obj).smoothing = true;
			_videoPlayer = new VideoPlayer(false);
			_bi = new BufferingIcon();
			_bi.scaleX = _bi.scaleY = 2;
			_videoPlayer._bufferingIcone = _bi;
			_videoPlayer.addEventListener(VideoPlayerEvent.VIDEO_READY, _videoReady);
			_videoPlayer.addEventListener(VideoPlayerEvent.VIDEO_COMPLETE, _videoComplete);
			_videoPlayer.addEventListener(VideoPlayerEvent.VIDEO_PLAY_HEAD_UPDATE, _videoPlayHeadUpdate);
			addChild(_videoPlayer);
			
			addChild(_obj.container);
			addChild(_bi);
			_obj.visible = true;

			stage.addEventListener(Event.RESIZE, _onResize);
			_onResize();
		}
		
		private function _videoPlayHeadUpdate(e:VideoPlayerEvent):void 
		{
			_playHeadPercentage = _videoPlayer.playHeadPercentage;
			_playHeadTime = _videoPlayer.playHeadTime;
			//trace("[SlideItem._videoPlayHeadUpdate] ",_playHeadPercentage);
			dispatchEvent(new Event(VideoPlayerEvent.VIDEO_PLAY_HEAD_UPDATE));
		}

		
		public function get playerVideo():VideoPlayer
		{
			return _videoPlayer;
		}
		public function get playHeadPercentage():Number
		{
			return _playHeadPercentage;
		}
		
		public function get playHeadTime():Number
		{
			return _playHeadTime;
		}
		
		public function _videoComplete(e:VideoPlayerEvent=null):void 
		{
			trace("[SlideItem._videoComplete] ");
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_COMPLETE));
			TweenMax.to(_obj, 1, { alpha:1} );			
		}
		
		private function _videoReady(e:VideoPlayerEvent):void 
		{
			
			_videoPlayer.removeEventListener(VideoPlayerEvent.VIDEO_READY, _videoReady);
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_READY));
			_videoPlayer.playVideo();
			TweenMax.to(_obj, 1, { alpha:0} );
			_onResize();
		}
		
		private function _onResize(e:Event=null):void 
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			var area:Rectangle = new Rectangle( 0, 0, sw, sh );
			DisplayUtils.fitIntoRect( _obj.container, area, true, Alignment.MIDDLE );
			if (_videoPlayer)
			{
				//DisplayUtils.fitIntoRect( _videoPlayer, area, true, Alignment.MIDDLE );
				_videoPlayer.x = _videoPlayer.y = 0;
				_videoPlayer.resize();
			}
			if (_bi)
			{
				_bi.x = sw / 2;
				_bi.y = sh / 2;
			}

		}
		
	}

}